#include "constants.h";

XfMissionChapterExecute={
	private ["_chapter_succeeded"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 1                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Starting Chapter"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];};

	_random_town_pos = DM_Chapter_Position;
	_random_town = DM_Chapter_Location;
	_random_town_size = _random_town select 3;
	_random_town_name = _random_town select 1;

	_pickup_updated = false;
	_assassinate_updated = false;
	_rescue_updated = false;
	_arrest_updated = false;
	_lead_prisoner_updated = false;
	_lead_hostage_updated = false;
	_activate_updated = false;
	_destroy_updated = false;
	_lead_updated = false;
	_takeover_updated = false;
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_success = true;

		_pickup = ["jip_money_found",false] call XNetGetJIP;
		_success = (_success AND _pickup);
		if (_pickup AND !_pickup_updated) then {["FindTheMoney","", "","",[],"Succeeded"] call XfMissionTask; _pickup_updated = true;};
		
		_assassinate = ["jip_traitor_killed",false] call XNetGetJIP;
		_success = (_success AND _assassinate);
		if (_assassinate AND !_assassinate_updated) then {["KillTheTraitor","", "","",[],"Succeeded"] call XfMissionTask; _assassinate_updated = true;};

		_hostage = ["jip_hostage_alive",true] call XNetGetJIP;
		if (!_hostage AND !_rescue_updated) then {["FreeHostage","", "","",[],"Failed"] call XfMissionTask; _rescue_updated = true;};
		
		_rescue = ["jip_hostage_free",false] call XNetGetJIP;
		_success = (_success AND (_rescue OR !_hostage));
		if (_rescue AND !_rescue_updated) then {
			["FreeHostage","", "","",[],"Succeeded"] call XfMissionTask;
			_rescue_updated = true;
			["jip_lead_hostage",false] call XNetSetJIP;
			["jip_hostage_alive",true] call XNetSetJIP;
			_positions set [ count _positions, getPos DM_Hostage];
			_pos = [];
			while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
				_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
				if (count _pos > 0) then {
					{
						if ((_pos distance _x) < 80) exitWith {_pos = [];};
					} foreach _positions;
				};
			};
			[DM_Hostage,group DM_Hostage,getPos DM_Hostage,"LEAD",["You took the hostage to safety!","You did your best...","jip_lead_hostage","jip_hostage_alive",[_pos,[20,20],0,false]],"doStop this; removeAllWeapons this;"] call XfMissionCreateMissionUnit;
			[false,["marker_hostage_dest", _pos, "ICON", "ColorBlue", [0.7,0.7],"Hostage Dest",0,"hd_end","",1]] call XfMissionMarker;
			["LeadHostage","Lead the hostage", "Lead the hostage to lost officer!","",[],"Created"] call XfMissionTask;
		};

		_lead_hostage = ["jip_lead_hostage",false] call XNetGetJIP;
		If (_rescue_updated && !_lead_hostage_updated && (_lead_hostage || !_hostage)) then {
			_lead_hostage_updated = true;
			if (_lead_hostage) then {
				["LeadHostage","", "","",[],"Succeeded"] call XfMissionTask;
			} else {
				["LeadHostage","", "","",[],"Failed"] call XfMissionTask;
			};
		};

		_prisoner = ["jip_pilot_alive",true] call XNetGetJIP;
		if (!_prisoner AND !_arrest_updated) then {["ArrestPilot","", "","",[],"Failed"] call XfMissionTask; _arrest_updated = true;};

		_arrest = ["jip_pilot_arrested",false] call XNetGetJIP;
		_success = (_success AND (_arrest OR !_prisoner));
		if (_arrest AND !_arrest_updated) then {
			["ArrestPilot","", "","",[],"Succeeded"] call XfMissionTask;
			_arrest_updated = true;
			["jip_lead_prisoner",false] call XNetSetJIP;
			["jip_prisoner_alive",true] call XNetSetJIP;
			_positions set [ count _positions, getPos DM_Arrested];
			_pos = [];
			while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
				_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
				if (count _pos > 0) then {
					{
						if ((_pos distance _x) < 80) exitWith {_pos = [];};
					} foreach _positions;
				};
			};
			[DM_Arrested,group DM_Arrested,getPos DM_Arrested,"LEAD",["You took the prisoner back!","You did your best...","jip_lead_prisoner","jip_prisoner_alive",[_pos,[20,20],0,false]],"doStop this; removeAllWeapons this;"] call XfMissionCreateMissionUnit;
			[false,["marker_prisoner_dest", _pos, "ICON", "ColorBlue", [0.7,0.7],"Prisoner Dest",0,"hd_end","",1]] call XfMissionMarker;
			["LeadPrisoner","Lead the prisoner", "Lead the prisoner to the church!","",[],"Created"] call XfMissionTask;
		};

		_lead_prisoner = ["jip_lead_prisoner",false] call XNetGetJIP;
		_dead_prisoner = !(["jip_prisoner_alive",true] call XNetGetJIP);
		If (_arrest_updated && !_lead_prisoner_updated && (_lead_prisoner || _dead_prisoner)) then {
			if (_lead_prisoner) then {
				["LeadPrisoner","", "","",[],"Succeeded"] call XfMissionTask;
			} else {
				["LeadPrisoner","", "","",[],"Failed"] call XfMissionTask;
			};
			_lead_prisoner_updated = true;
		};
		
		_activate = ["jip_generator_active",false] call XNetGetJIP;
		_success = (_success AND _activate);
		if (_activate AND !_activate_updated) then {["ActivateGenerator","", "","",[],"Succeeded"] call XfMissionTask; _activate_updated = true;};

		_destroy = ["jip_tower_is_destroyed",false] call XNetGetJIP;
		_success = (_success AND _destroy);
		if (_destroy AND !_destroy_updated) then {["DestroyTower","", "","",[],"Succeeded"] call XfMissionTask; _destroy_updated = true;};

		_officer = ["jip_soldier_alive",true] call XNetGetJIP;
		if (!_officer AND !_lead_updated) then {["LeadOfficer","", "","",[],"Failed"] call XfMissionTask; _lead_updated = true;};

		_lead = ["jip_lead_soldier",false] call XNetGetJIP;
		_success = (_success AND (_lead OR !_officer));
		if (_lead AND !_lead_updated) then {["LeadOfficer","", "","",[],"Succeeded"] call XfMissionTask; _lead_updated = true;};
		
		_takeover = ["jip_take_over",false] call XNetGetJIP;
		_success = (_success AND _takeover);
		if (_takeover AND !_takeover_updated) then {
			["TakeOverBunker","", "","",[],"Succeeded"] call XfMissionTask;
			_takeover_updated = true;
			DM_Flag setFlagTexture "\ca\data\Flag_usa_co.paa";
		};
		
		if (_success) then {
			_chapter_succeeded = True;
			DM_Mission_Chapter_CanRun = false;
		};
		// diag_log Format["(%1)[%2]: %3: %4, %5, %6, %7, %8, %9, %10, %11",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute",_pickup,_assassinate,_rescue,_arrest,_activate,_destroy,_lead,_success];
	};
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Mission canceled!"];
	};
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_grp"];
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Starting Initialization"];

	// We take towns AND villages
	_random_town = [true, 150] call XfMissionGetRandomTown;
	while {(_random_town_pos distance FLAG_BASE) < 700} do {
		_random_town = [true] call XfMissionGetRandomTown;
	};
	if ((count _random_town) <= 0) exitWith {"Could not pick a random town" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	_random_town_pos = _random_town select 0;
	
	_positions = [];
	
	DM_Chapter_Position = _random_town_pos;
	DM_Chapter_Location = _random_town;
	_random_town_size = _random_town select 3;
	_random_town_name = _random_town select 1;
	diag_log Format["(%1)[%2]: %3: %4 %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Selected Town:",_random_town];
	if (_random_town_size < 200) then { _random_town_size = 200; };
	// This indicates where the mission takes place
	[_random_town_pos,_random_town_size,_random_town select 1] call XfMissionCurrentPosition;
	
	// We search a spot to place the teleporter which is around the town on a road (to avoid mountains and forests)
	_teleporter = [_random_town_pos] call XfMissionRandomStartTeleporter;
	
	_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
	_positions set [count _positions, _pos];
	["jip_money_found",false] call XNetSetJIP;
	["EvMoney",_pos,"PICKUP",["Pickup Money","You found the money","jip_money_found","Take the money",True]] call XfMissionCreateMissionObject;
	[false,["marker_pickup", _pos, "ICON", "ColorBlue", [0.7,0.7],"Pickup",0,"hd_objective","",1]] call XfMissionMarker;
	["FindTheMoney","Find the stolen money", "You need to find the stolen money and take it back to the base.","",[],"Created"] call XfMissionTask;
	
	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_positions set [count _positions, _pos];
	["jip_traitor_killed",false] call XNetSetJIP;
	_grp = [d_enemy_side] call x_creategroup;
	["RU_Profiteer2",_grp,_pos,"ASSASSINATE",["You killed the traitor","jip_traitor_killed"],"doStop this;"] call XfMissionCreateMissionUnit;
	[false,["marker_assassinate", _pos, "ICON", "ColorRed", [0.7,0.7],"Assassinate",0,"hd_objective","",1]] call XfMissionMarker;
	["KillTheTraitor","Kill the traitor", "Find and kill the traitor who sold informations about our operations!","",[],"Created"] call XfMissionTask;
	
	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_positions set [count _positions, _pos];
	["jip_hostage_free",false] call XNetSetJIP;
	["jip_hostage_alive",true] call XNetSetJIP;
	_grp = ["CIV"] call x_creategroup;
	DM_Hostage = ["RU_Worker1",_grp,_pos,"RESCUE",["You freed the hostage, congratulations!","You did your best to save the hostage...","jip_hostage_free","jip_hostage_alive"],"doStop this;"] call XfMissionCreateMissionUnit;
	[false,["marker_rescue", _pos, "ICON", "ColorBlue", [0.7,0.7],"Free",0,"hd_objective","",1]] call XfMissionMarker;
	["FreeHostage","Free the hostage", "Free the hostage to win the villages to our cause!","",[],"Created"] call XfMissionTask;
	
	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_positions set [count _positions, _pos];
	["jip_pilot_arrested",false] call XNetSetJIP;
	["jip_pilot_alive",true] call XNetSetJIP;
	_grp = [d_enemy_side] call x_creategroup;
	DM_Arrested = ["RU_Soldier_Pilot",_grp,_pos,"ARREST",["You caught the pilot, congratulations!","Damn, you killed the pilot....","jip_pilot_arrested","jip_pilot_alive"],"doStop this;"] call XfMissionCreateMissionUnit;
	[false,["marker_arrest", _pos, "ICON", "ColorRed", [0.7,0.7],"Arrest",0,"hd_objective","",1]] call XfMissionMarker;
	["ArrestPilot","Arrest the pilot", "The pilot of a recon aircraft did crash near the town, find him and arrest him! Use force if necessary! But don't kill him, we need the intel!","",[],"Created"] call XfMissionTask;

	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_positions set [count _positions, _pos];
	["jip_generator_active",false] call XNetSetJIP;
	["PowGen_Big",_pos,"ACTIVATE",["The generator is on","The generator is off","jip_generator_active"]] call XfMissionCreateMissionObject;
	[false,["marker_activate", _pos, "ICON", "ColorBlue", [0.7,0.7],"Activate",0,"hd_objective","",1]] call XfMissionMarker;
	["ActivateGenerator","Activate the generator", "Restore the power to the town, this will allow our radars to operate again!","",[],"Created"] call XfMissionTask;

	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_positions set [count _positions, _pos];
	["jip_tower_is_destroyed",False] call XNetSetIP;
	["Land_telek1",_pos,"DESTROY",["The Tower is destroyed!","jip_tower_is_destroyed",True,1,["PipeBomb"]]] call XfMissionCreateMissionObject;
	[false,["marker_destroy", _pos, "ICON", "ColorGreen", [0.7,0.7],"Destroy",0,"hd_objective","",1]] call XfMissionMarker;
	["DestroyTower","Destroy the tower", "The enemy is using the radio tower to jam our frequencies, destroy it!","",[],"Created"] call XfMissionTask;
	
	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_positions set [count _positions, _pos];
	_pos2 = [];
	while { count _pos2 <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos2 = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos2 > 0) then {
			{
				if ((_pos2 distance _x) < 20) exitWith {_pos2 = [];};
			} foreach _positions;
		};
	};
	["jip_lead_soldier",false] call XNetSetJIP;
	["jip_soldier_alive",true] call XNetSetJIP;
	_grp = [d_own_side] call x_creategroup;
	["USMC_Soldier_Officer",_grp,_pos,"LEAD",["You brought our soldier back, congratulations!","You did your best...","jip_lead_soldier","jip_soldier_alive",[_pos2,[20,20],0,false]],"doStop this; removeAllWeapons this;"] call XfMissionCreateMissionUnit;
	[false,["marker_lead", _pos, "ICON", "ColorBlue", [0.7,0.7],"Lead",0,"hd_objective","",1]] call XfMissionMarker;
	[false,["marker_lead_dest", _pos2, "ICON", "ColorBlue", [0.7,0.7],"Extraction",0,"hd_end","",1]] call XfMissionMarker;
	["LeadOfficer","Bring the officer back", "One of our officers is hiding in town from the enemy. Bring him back to the Extraction point","",[],"Created"] call XfMissionTask;
	
	_pos = [];
	while { count _pos <= 0  && DM_Mission_Mission_CanRun && DM_Mission_Chapter_CanRun} do {
		_pos = ([_random_town_pos, _random_town_size] call XfGetRanPointCircle) findEmptyPosition[0 , 30];
		if (count _pos > 0) then {
			{
				if ((_pos distance _x) < 20) exitWith {_pos = [];};
			} foreach _positions;
		};
	};
	_bunker = ["Land_fortified_nest_big",[_pos,random(360)],"REGISTER",[],"","CAN_COLLIDE"] call XfMissionCreateMissionObject;
	_pos = _bunker modelToWorld [-0.17,0.2,-0.65];
	DM_Flag = ["FlagCarrierRU",[_pos,0],"TAKEOVER",[7,"The bunker has been taken over by the allies!","Damn, they took the bunker back!","jip_take_over",20,d_side_enemy,d_side_player],"","CAN_COLLIDE"] call XfMissionCreateMissionObject;
	[false,["marker_takeover", _pos, "ICON", "ColorRed", [0.7,0.7],"Take Over",0,"hd_objective","",1]] call XfMissionMarker;
	["TakeOverBunker","Take Over the Bunker", "Take possession of the enemy bunker! You need to hold it for 20 seconds.","",[],"Created"] call XfMissionTask;

};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSuccess","Starting"];
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterFailure","Starting"];
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	[] call XfMissionCleanObjects;
	[] call XfMissionCleanMarkers;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;